home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / FOPEN.C < prev    next >
Text File  |  1986-05-18  |  3KB  |  96 lines

  1. /* 1.2  12-17-85                        (fopen.c)
  2.  ************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1984        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10. #include "errno.h"
  11. #include "stdio.h"
  12.  
  13. /************************************************************************/
  14.     METACHAR
  15. fclose(fp)        /* flush the FILE fp buffer, and close the file.
  16.                Return EOF on error, NULL otherwise.     */
  17. /*----------------------------------------------------------------------*/
  18. FAST FILE *fp;
  19. {
  20.     int err;
  21.     METACHAR fflush();
  22.  
  23.     err = NULL;
  24.     if (fp->_flags)
  25.     {    if (fp->_flags & _DIRTY)   /* if modifed, flush buffer     */
  26.             err = fflush(fp);
  27.         err |= close(fp->_unit);
  28.         if (fp->_flags & _ALLBUF)    /* put the buffer back in */
  29.             liberate(fp->_buff, BUFSIZ);    /* the free list. */
  30.     }
  31.     fp->_flags = NULL;
  32.     return err;
  33. }
  34.  
  35. /************************************************************************/
  36.     METACHAR
  37. fflush(fp)        /* Write characters in the FILE fp buffer onto
  38.                the file.  Return EOF on error.        */
  39. /*----------------------------------------------------------------------*/
  40. FILE *fp;
  41. {
  42.     METACHAR flush();
  43.  
  44.     return flush(fp, EOF);
  45. }
  46. /*\p*********************************************************************/
  47.     FILE *
  48. fopen(name, type)    /* Open name file according to given type. Return
  49.                pointer to stream, or NULL on failure.    */
  50. /*----------------------------------------------------------------------*/
  51. STRING name, type;
  52. {
  53.     FAST FILE *fp;
  54.     FAST BOOL t;
  55.     FAST int fd;
  56.  
  57.     for (fp = IObuffs; fp->_flags; ) /* look for a blank flag field. */
  58.         if (++fp >= IObuffs + MAXSTREAM)
  59.             return (NULL);
  60.  
  61.     t = (type[1] IS '+');
  62.     switch (*type)
  63.     {    case 'r':
  64.             if ((fd = open(name, t? _RDWRIT : _RDONLY)) IS EOF)
  65.                 return (NULL);
  66.             break;
  67.         case 'w':
  68.             if ((fd = creat(name, PMODE)) IS EOF)
  69.                 return (NULL);
  70.             if (t)
  71.             {    close(fd);
  72.                 fd = open(name, _RDWRIT);
  73.             }
  74.             break;
  75.         case 'a':
  76.             if ((fd = open(name, t? _RDWRIT : _WRONLY)) IS EOF)
  77.             {    if ((fd = creat(name, PMODE)) IS EOF)
  78.                     return (NULL);
  79.  
  80.                 close(fd);
  81.                 fd = open(name, t ? _RDWRIT : _WRONLY);
  82.                 break;
  83.             }
  84.             lseek(fd, 0L, 2);
  85.             break;
  86.         default:
  87.             errno = EINVAL;
  88.             return NULL;
  89.     }
  90.     fp->_unit = fd;
  91.     fp->_flags = _BUSY;
  92.     fp->_buflen = BUFSIZ;
  93.     fp->_buff = fp->_bend = fp->_bptr = NULL;
  94.     return (fp);
  95. }
  96.